//
//  PlayView.m
//  DolphinMusic
//
//  Created by dllo on 15/3/29.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "PlayView.h"
#import "MusicPlayViewController.h"

@implementation PlayView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc
{
    [_titleLabel release];
    [_authorLabel release];
    [_timeLabel release];
    [_playButton release];
    [_imageView release];
    [_lastButton release];
    [_nextButton release];;
    [_imageView1 release];;
    [_imageView2 release];;
    [_imageView3 release];;
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame imageName:(NSString *)imageStr
{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.height, frame.size.height)];
        self.imageView.image = [UIImage imageNamed:imageStr];
        self.imageView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [self.imageView addGestureRecognizer:tap];
        [tap release];
        
        
        [self addSubview:self.imageView];
        [_imageView release];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.height + 10.0 / 375 * frame.size.width, 0, 210.0 / 375 * frame.size.width -  (frame.size.height + 10.0 / 375 * frame.size.width), frame.size.height / 3)];
        
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        self.titleLabel.textColor = [UIColor whiteColor];
        [self addSubview:self.titleLabel];
        [_titleLabel release];
        
        self.authorLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.frame.origin.x, frame.size.height / 3, self.titleLabel.frame.size.width, frame.size.height / 3)];
        self.authorLabel.font = [UIFont systemFontOfSize:13];
        self.authorLabel.textColor = [UIColor whiteColor];
        self.authorLabel.alpha = 0.6;
        [self addSubview:self.authorLabel];
        [_authorLabel release];
        
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.frame.origin.x, frame.size.height * 2 / 3, self.authorLabel.frame.size.width, frame.size.height / 3)];
        self.timeLabel.font = [UIFont systemFontOfSize:13];
        self.timeLabel.textColor = [UIColor whiteColor];
        self.timeLabel.alpha = 0.6;
        [self addSubview:self.timeLabel];
        [_timeLabel release];
        
        
        self.lastButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.lastButton.frame = CGRectMake(200.0 / 375 * frame.size.width, 0, 55.0 / 375 * frame.size.width, frame.size.height);
        
        self.imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(self.lastButton.frame.size.width / 2 - 15, self.lastButton.frame.size.height / 2 - 15, 30, 30)];
        self.imageView1.image = [UIImage imageNamed:@"aboveMusic.png"];
        [self.lastButton addSubview:self.imageView1];
        [_imageView1 release];
    
        [self addSubview:self.lastButton];
        [self.lastButton addTarget:self action:@selector(lastButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        self.playButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.playButton.frame = CGRectMake(255.0 / 375 * frame.size.width, 0, 55.0 / 375 * frame.size.width, frame.size.height);
        self.imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(self.playButton.frame.size.width / 2 - 20, self.playButton.frame.size.height / 2 - 15, 40, 30)];
        self.imageView2.image = [UIImage imageNamed:@"play1.png"];
        [self.playButton addSubview:self.imageView2];
        [_imageView2 release];
        
        [self addSubview:self.playButton];
        
        [self.playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        self.nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.nextButton.frame = CGRectMake(305.0 / 375 * frame.size.width, 0, 55.0 / 375 * frame.size.width, frame.size.height);
        self.imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(self.nextButton.frame.size.width / 2 - 15, self.nextButton.frame.size.height / 2 - 15, 30, 30)];
        self.imageView3.image = [UIImage imageNamed:@"nextMusic.png"];
        [self.nextButton addSubview:self.imageView3];
        [_imageView3 release];
        
        
        [self.nextButton addTarget:self action:@selector(nextButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.nextButton];
        
    }
    return self;
}


- (void)tap:(UITapGestureRecognizer *)tap
{
    MusicPlayViewController *musicVC = [MusicPlayViewController shareInstance];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:musicVC];
    navi.navigationBarHidden = YES;
    [self.target presentViewController:navi animated:YES completion:^{
    }];
}


- (void)nextButtonAction:(UIButton *)button
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"下一曲" object:nil];
}


- (void)playButtonAction:(UIButton *)button
{
    MusicPlayViewController *musicVC = [MusicPlayViewController shareInstance];
    if (musicVC.player.state == STKAudioPlayerStatePlaying) {

        self.imageView2.image = [UIImage imageNamed:@"play1.png"];
        
        [musicVC.player pause];
    }
    else if (musicVC.player.state == STKAudioPlayerStatePaused) {

        self.imageView2.image = [UIImage imageNamed:@"pause.png"];
        [musicVC.player resume];
        
}
}

- (void)lastButtonAction:(UIButton *)button
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"上一曲" object:nil];
}

@end
